package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentPersonGroup;
import com.mxpio.erp.common.equipment.entity.EquipmentPersonGroupDetail;
import com.mxpio.erp.common.equipment.service.EquipmentPersonGroupService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 人员群组管理Service业务层处理
 *
 * @author Cheng
 * @date 2023-06-28
 */
@Service
public class EquipmentPersonGroupServiceImpl extends BaseServiceImpl<EquipmentPersonGroup> implements EquipmentPersonGroupService {

    @Override
    @Transactional(readOnly = true)
    public List<String> selectByUsername(String username) {
        List<EquipmentPersonGroupDetail> groupDetails = JpaUtil.linq(EquipmentPersonGroupDetail.class).equal("userId", username).list();
        List<String> list = groupDetails.stream().map(item -> item.getGroupId()).collect(Collectors.toList());
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public Result<List<EquipmentPersonGroup>> getGroupListByUserId(String userId, Criteria criteria) {
        List<EquipmentPersonGroup> groupList = JpaUtil.linq(EquipmentPersonGroup.class)
                .exists(EquipmentPersonGroupDetail.class)
                    .equalProperty("groupId", "id")
                    .equal("userId", userId)
                .end()
                .where(criteria)
                .list();
        return Result.OK(groupList);
    }
}
